package com.example.miaopin.fragment;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.example.miaopin.MainActivity;
import com.example.miaopin.PhotoUtilChange;
import com.example.miaopin.R;
import com.example.miaopin.adapter.ContentAdapter;
import com.example.miaopin.utils.FileUtils;
import com.example.miaopin.utils.SPUtil;
import com.viewpagerindicator.TabPageIndicator;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import circleimageview.CircleImageView;

/**
 * @author Administrator 互刷留言fragment
 */
public class HushuaL extends BaseFragment {

	private String[] CONTENT;
	private static final int CODE_ZOOM_PHOTOT = 200;
	private static final int takePhotoCode = 201;
	private static final int imageCod = 202;
	private String sdCardPath = Environment.getExternalStorageDirectory().getAbsolutePath();
	private File tempFile = new File(sdCardPath + "/" + "tempFile.jpg");
	private CircleImageView imageView;

	private RefreshLayout mRefreshLayout;
	private static SPUtil spUtil;

	public void setmRefreshLayout(RefreshLayout mRefreshLayout) {
		this.mRefreshLayout = mRefreshLayout;
	}

	@Override
	protected View initview() {
		if (!tempFile.exists()) {
			FileUtils.createFile(tempFile.getAbsolutePath());
		}
		spUtil = new SPUtil(mContext);
		CONTENT = mContext.getResources().getStringArray(R.array.hushua);
		view = inflater1(R.layout.hushua);
		imageView = (CircleImageView) view.findViewById(R.id.xiangpian);
		imageView.setOnClickListener(this);
		List<Fragment> fragments = new ArrayList<Fragment>();
		YaunFragment yaunFragment = new YaunFragment();
		JiluFragment jiluFragment = new JiluFragment();
		fragments.add(yaunFragment);
		fragments.add(jiluFragment);
		ContentAdapter adapter = new ContentAdapter(((MainActivity) mContext).getSupportFragmentManager(), CONTENT);
		ViewPager pager = (ViewPager) view.findViewById(R.id.viewpaget__hs);
		pager.setAdapter(adapter);

		TabPageIndicator indicator = (TabPageIndicator) view.findViewById(R.id.indicator_hs);
		indicator.setViewPager(pager);
		EditText editText = (EditText) view.findViewById(R.id.zhuangbi);
		// EditTextViewUtils.showOffLabel(editText);
		// KeyBoardUtils.openKeybord(editText, mContext);
		return view;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.xiangpian:
			PhotoUtilChange.getPhotoDialog((MainActivity) mContext, this, takePhotoCode, imageCod, tempFile);

			break;

		default:
			break;
		}

	}

	@Override
	protected void setTtitle(String string) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void setImage(Bitmap bitmap) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case takePhotoCode:// 拍照
			// 2.处理拍照的结果-->去裁剪
			Log.i("aini", "回调成功");
			if (PhotoUtilChange.getLocalImage(tempFile, 1000, 10000) != null) {

				spUtil.putBoolean("aini", true);
				PhotoUtilChange.onPhotoFromCamera((MainActivity) mContext, this, CODE_ZOOM_PHOTOT,
						tempFile.getAbsolutePath(), 1, 1);
			}
			break;
		case imageCod:// 从相册选择
			// 2.处理从相册选择的结果-->去裁剪
			if (data != null) {

				Log.i("aini", "回调成功");
				PhotoUtilChange.onPhotoFromPick((MainActivity) mContext, this, CODE_ZOOM_PHOTOT,
						tempFile.getAbsolutePath(), data, 50, 50);
			}
			break;
		case CODE_ZOOM_PHOTOT:// 裁剪
			if (data != null) {

				// 3.裁剪完成
				// Bitmap zoomBitMap = PhotoUtilChange.getZoomBitMap(data,
				// (MainActivity) mContext);
				Bitmap zoomBitMap = PhotoUtilChange.getLocalImage(new File(tempFile.getAbsolutePath() + "temp1.jpg"),
						1000, 10000);
				// 4.修改头像
				imageView.setImageBitmap(zoomBitMap);
				;

				if (spUtil.getBoolean("aini", false)) {
 
					spUtil.putBoolean("aini", false);
					//
					mRefreshLayout.change(true);
				}
			}

			break;
		default:
			break;
		}
	}

	public interface RefreshLayout {
		public void change(boolean a);
	}
}
